<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>elasticsearch-shard | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'shard-tool.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/shard-tool.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/shard-tool.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/shard-tool.html" rel="nofollow" target="_blank">../en/shard-tool.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="commands.html">Command line tools</a></span>
»
<span class="breadcrumb-node">elasticsearch-shard</span>
</div>
<div class="navheader">
<span class="prev">
<a href="setup-passwords.html">« elasticsearch-setup-passwords</a>
</span>
<span class="next">
<a href="syskeygen.html">elasticsearch-syskeygen »</a>
</span>
</div>
<div class="chapter">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="shard-tool"></a>elasticsearch-shard<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/commands/shard-tool.asciidoc">edit</a>
</h2>
</div></div></div>
<p>In some cases the Lucene index or translog of a shard copy can become corrupted.
The <code class="literal">elasticsearch-shard</code> command enables you to remove corrupted parts of the
shard if a good copy of the shard cannot be recovered automatically or restored
from backup.</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>You will lose the corrupted data when you run <code class="literal">elasticsearch-shard</code>.  This tool
should only be used as a last resort if there is no way to recover from another
copy of the shard or restore a snapshot.</p>
</div>
</div>
<h3>
<a id="_synopsis_8"></a>Synopsis<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/commands/shard-tool.asciidoc">edit</a>
</h3>
<div class="pre_wrapper lang-shell">
<pre class="programlisting prettyprint lang-shell">bin/elasticsearch-shard remove-corrupted-data
  ([--index &lt;Index&gt;] [--shard-id &lt;ShardId&gt;] | [--dir &lt;IndexPath&gt;])
  [--truncate-clean-translog]
  [-E &lt;KeyValuePair&gt;]
  [-h, --help] ([-s, --silent] | [-v, --verbose])</pre>
</div>
<h3>
<a id="_description_8"></a>Description<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/commands/shard-tool.asciidoc">edit</a>
</h3>
<p>When Elasticsearch detects that a shard’s data is corrupted, it fails that shard copy and
refuses to use it. Under normal conditions, the shard is automatically recovered
from another copy. If no good copy of the shard is available and you cannot
restore one from a snapshot, you can use <code class="literal">elasticsearch-shard</code> to remove the
corrupted data and restore access to any remaining data in unaffected segments.</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>Stop Elasticsearch before running <code class="literal">elasticsearch-shard</code>.</p>
</div>
</div>
<p>To remove corrupted shard data use the <code class="literal">remove-corrupted-data</code> subcommand.</p>
<p>There are two ways to specify the path:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
Specify the index name and shard name with the <code class="literal">--index</code> and <code class="literal">--shard-id</code>
options.
</li>
<li class="listitem">
Use the <code class="literal">--dir</code> option to specify the full path to the corrupted index or
translog files.
</li>
</ul>
</div>
<h4>
<a id="_removing_corrupted_data"></a>Removing corrupted data<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/commands/shard-tool.asciidoc">edit</a>
</h4>
<p><code class="literal">elasticsearch-shard</code> analyses the shard copy and provides an overview of the
corruption found. To proceed you must then confirm that you want to remove the
corrupted data.</p>
<div class="warning admon">
<div class="icon"></div>
<div class="admon_content">
<p>Back up your data before running <code class="literal">elasticsearch-shard</code>. This is a destructive
operation that removes corrupted data from the shard.</p>
</div>
</div>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">$ bin/elasticsearch-shard remove-corrupted-data --index twitter --shard-id 0


    WARNING: Elasticsearch MUST be stopped before running this tool.

  Please make a complete backup of your index before using this tool.


Opening Lucene index at /var/lib/elasticsearchdata/nodes/0/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/

 &gt;&gt; Lucene index is corrupted at /var/lib/elasticsearchdata/nodes/0/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/

Opening translog at /var/lib/elasticsearchdata/nodes/0/indices/P45vf_YQRhqjfwLMUvSqDw/0/translog/


 &gt;&gt; Translog is clean at /var/lib/elasticsearchdata/nodes/0/indices/P45vf_YQRhqjfwLMUvSqDw/0/translog/


  Corrupted Lucene index segments found - 32 documents will be lost.

            WARNING:              YOU WILL LOSE DATA.

Continue and remove docs from the index ? Y

WARNING: 1 broken segments (containing 32 documents) detected
Took 0.056 sec total.
Writing...
OK
Wrote new segments file "segments_c"
Marking index with the new history uuid : 0pIBd9VTSOeMfzYT6p0AsA
Changing allocation id V8QXk-QXSZinZMT-NvEq4w to tjm9Ve6uTBewVFAlfUMWjA

You should run the following command to allocate this shard:

POST /_cluster/reroute
{
  "commands" : [
    {
      "allocate_stale_primary" : {
        "index" : "index42",
        "shard" : 0,
        "node" : "II47uXW2QvqzHBnMcl2o_Q",
        "accept_data_loss" : false
      }
    }
  ]
}

You must accept the possibility of data loss by changing the `accept_data_loss` parameter to `true`.

Deleted corrupt marker corrupted_FzTSBSuxT7i3Tls_TgwEag from /var/lib/elasticsearchdata/nodes/0/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/</pre>
</div>
<p>When you use <code class="literal">elasticsearch-shard</code> to drop the corrupted data, the shard’s
allocation ID changes. After restarting the node, you must use the
<a class="xref" href="cluster-reroute.html" title="Cluster reroute API">cluster reroute API</a> to tell Elasticsearch to use the new ID.
The <code class="literal">elasticsearch-shard</code> command shows the request that you need to submit.</p>
<p>You can also use the <code class="literal">-h</code> option to get a list of all options and parameters
that the <code class="literal">elasticsearch-shard</code> tool supports.</p>
<p>Finally, you can use the <code class="literal">--truncate-clean-translog</code> option to truncate the
shard’s translog even if it does not appear to be corrupt.</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="setup-passwords.html">« elasticsearch-setup-passwords</a>
</span>
<span class="next">
<a href="syskeygen.html">elasticsearch-syskeygen »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>